// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/ast/base.h>

namespace cantor::parser {

    nlohmann::ordered_json LocationInfo::to_json() const {
        nlohmann::ordered_json j;
        j["location"] = location;
        j["len"] = len;
        return j;
    }

    std::ostream &operator<<(std::ostream &os, const LocationInfo &loc) {
        os << "[" << loc.location << ":" << loc.len << "]";
        return os;
    }

    nlohmann::ordered_json Node::to_json_impl() const {
        nlohmann::ordered_json j;
        j["node_type"] = NodeType_Name(node_type);
        j["is_complex"] = is_complex;
        j["location"] = location.to_json();
        return j;
    }

    std::string Node::to_string() const {
        std::ostringstream os;
        to_stream(os);
        return os.str();
    }

    std::ostream &operator<<(std::ostream &os, const Node *node) {
        if (node != nullptr) {
            node->to_stream(os);
        }
        return os;
    }

    std::ostream &operator<<(std::ostream &os, const Node &node) {
        node.to_stream(os);
        return os;
    }

    bool Node::is_complex_node() {
        if (is_complex) {
            return true;
        }
        return false;
    }

    void Node::print() const {
        std::cout << "type:" << node_type << std::endl;
    }
}  // namespace cantor::parser
